Docker Container CLI Cheat Sheet

As part of my back to basics series I thought I would create an article on base Docker container commands.  Obviously, there are a lot more ways to dig into the CLI but this is a good set of commands to start with.

Screenshot from the Docker Container CLI Commands walkthrough at step 1; the adjacent article text explains the docker run command with an image name placeholder.

$ docker run -it <image name>

This will allow you to go into an interactive terminal and issue traditional commands.

My example is ubuntu, as you can see I ran the simple #ps -elf command and the # cat /etc/*release* to receive output from the ubuntu OS.

Screenshot from the Docker Container CLI Commands walkthrough at step 2, showing CONTAINER ID; the adjacent article text explains that my example is ubuntu, as you can see I ran the simple #ps -elf command and the # cat /etc/*release* to receive output from the ubuntu OS.

$ docker run -d <image name> sleep <number>

-d allows us to start a container in detached mode so it doesn’t consume our terminal

The sleep command gives the container an action that will prevent it from immediately exiting and stopping

Screenshot from the Docker Container CLI Commands walkthrough at step 3, showing CONTAINER ID; the adjacent article text explains that the sleep command gives the container an action that will prevent it from immediately exiting and stopping.

$ docker run -td <image>

If you do not wish to use a command like sleep, you can use -td and it will start a container and keep it running

Screenshot from the Docker Container CLI Commands walkthrough at step 4, showing CONTAINER ID; the adjacent article text explains that if you do not wish to use a command like sleep, you can use -td and it will start a container and keep it running.

$ docker ps

This command shows you all running containers on your host

Screenshot from the Docker Container CLI Commands walkthrough at step 5, showing CONTAINER ID; the adjacent article text explains the docker stop command with a container ID or name placeholder.

$ docker stop <container_ID or NAMES>

This command allows you to stop an active container

Screenshot from the Docker Container CLI Commands walkthrough at step 6, showing CONTAINER ID; the adjacent article text explains that this command allows you to stop an active container.

$ docker ps -a

This shows you all containers on the host, even those in an Exited state

Screenshot from the Docker Container CLI Commands walkthrough at step 7, showing CONTAINER ID; the adjacent article text explains the docker start command with a container ID or name placeholder.

$ docker start <Container_ID or NAMES>

This will allow you to restart a container that is already in an Exited state

Screenshot from the Docker Container CLI Commands walkthrough at step 8, showing CONTAINER ID; the adjacent article text explains that this will allow you to restart a container that is already in an Exited state.

$ Docker rm <Container_ID or NAMES>

This command allows you to remove an exited container and reclaim the resources it is using.  ***container must be stopped to remove***

Screenshot from the Docker Container CLI Commands walkthrough at step 9, showing CONTAINER ID; the adjacent article text explains that this command allows you to remove an exited container and reclaim the resources it is using.

$docker logs <Container_ID or NAMES>
Will show you the output from the logs to help in troubleshooting

Screenshot from the Docker Container CLI Commands walkthrough at step 10, showing CONTAINER ID; the adjacent article text explains that this command allows you to remove an exited container and reclaim the resources it is using.

$ docker rename <existing_name> <new_name>

This will allow you to rename the container

Screenshot from the Docker Container CLI Commands walkthrough at step 11, showing CONTAINER ID; the adjacent article text explains that this will allow you to rename the container.

$ docker pause <container_ID or NAMES>

This will allow you to pause a running container

Screenshot from the Docker Container CLI Commands walkthrough at step 12; the adjacent article text explains that this will allow you to pause a running container.

$ docker run -d –name <NAME> -p 8080:8080 -p5000:5000 jenkins:2.60.3

-d will have container deployed in detach mode allowing you to still use your terminal

–name will allow you to name your app

-p this is port mapping from container to host so we are exposing 8080 publicly, which will be confirmed when I put the host IP:8080 into a URL and let the prompt for my Jenkins credentials

jenkins:2.60.3 this applies the tag 2.60.3 which is the latest version of Jenkins in case a new update comes out tomorrow changing the latest version

Screenshot from the Docker Container CLI Commands walkthrough at step 13; the adjacent article text explains that proof the above commands worked.

Proof the above commands worked

Summary:
We covered a lot of base container commands here in this article.  If you go out to docs.docker.com you will find plenty more to play with.  As always, I hope y’all found this article helpful.

Keep exploring

Choose your next step

Continue with the path that best matches the architecture or operating challenge in front of you.

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading