Introduction to docker commands
For anyone who has interacted with a certain tool in your development career, you know that there are those commands or keyboard shortcuts that you have at your fingertips that make your life easier while using that tool. It is with that same thought that I present to you the commands any new docker learner should have at one’s finger tips.
docker ps
lists all running containers on your computer. Additionally run the command below to list all containers in whatever state they are in.
docker ps -a
docker rm <container ID/name>
remove a container whose ID/name is specified in place of<container ID/name>
. At times you need to force remove a container, for that run:
docker rm -f <container ID/name>
docker start <container ID/name>
starts a stopped container or one that is not running whose ID/name is specified in place of<container ID/name>
in the above command. If, say you need an interactive shell of that container, run the command below to provide for that.
docker start -ai <container ID/name>
docker stop <container ID/name>
stops a running container whose ID/name is specified in place of<container ID/name>
. When this command is run, it takes 10 seconds by default to stop that stated container, but you can specify a longer or shorter time with the -t flag followed by time in seconds. something like:
docker stop kd778373jjdjsh2998rgghhsg -t 30
docker logs <container ID/name>
fetches logs of a container whose ID/name is specified in place of<container ID/name>
. At times you have an application running in a container and given its setup, you have no way of accessing the container shell to inspect the logs incase the container is still running or exited. This command is useful in many situations such as debugging, logging and monitoring.docker exec <container ID/name> <command to run in container>
. say you have a running container and you need to run a certain command in it; that is where this command comes into play. This docker command allows you to run a given shell command in that container. For instance, to print the current working directory of a container to your computer’s terminal, run:
docker exec <container name/ID> pwd
docker images
lists all images stored locally on your computer. Additionally, in your terminal run the command below to list all images, including intermediate images.:
docker images -a
docker rmi <image ID>
remove an image whose ID is specified in place of<image ID>
. Just like containers, at times you need to force remove an image. For that, run:
docker rmi -f <image ID>
docker port <container ID/name>
this lists all the port mappings of the given container.docker run <image ID>
this creates a container from the image specified in place of<image ID>
. Additionally if you need some further customization to thedocker run
command, flags such as-it
give you an interactive shell through which you interact with the docker container. So the command will look likedocker run -it <image ID>
. There's a bunch of other flags you can apply to this docker command for a more customized container creation process. Run the command below for more options to run with this command.
docker run -it <image ID> --help
docker build .
this builds a docker image from a Dockerfile. In your terminal, run this command in the folder that contains the Dockerfile, Do not forget the period at the end of the command as that specifies the context docker uses while building the docker image. If the Dockerfile does not reside in the same folder where you are running the command, add the-f
flag to specify the folder/URL where the Dockerfile resides.
docker build -f /folder/where/the/Dockerfile/resides/ .
docker pull
this command pulls an image from the docker registry to your computer. For instance to pull a postgres image, you run:
docker pull postgres
If you want to pull a specific version of an image, you add the version of that image like:
docker pull postgres:9.6
docker push <docker ID/image to be pushed>
this will push a locally available image to the docker registry.
docker push david/database_image
Where david
is the repository name, and database_image
is the image to be pushed to the docker registry.
docker login
this logs a user into the docker registry from the terminal. Additional flags can be passed with this command to provide for the password-p
and username-u
.
docker login -u david -p password
The above command logs in user david
with password password
docker tag
this tags a local image with a tag that the docker registry will use instead of the one used locally. This tag can also be the same as the local tag; there’s no harm in that.
docker tag david:database_image $DOCKER_ID_USER/database_image
In the above command, david:database_image
is the local image name/tag. $DOCKER_ID_USER/database_image
is the tag docker registry will use. DOCKER_ID_USER
environment variable should be set to your docker username before running the tagging command, or do not set it at all and instead replace it with the docker name directly.