A "work in progress"-list with the Docker commands I use the most
Since the start of 2019, I have been using Docker containers in the projects I work on. Working with a command line tool (Windows PowerShell, Shell, etc.) is essential when creating new containers. Below is a list of the commands I use the most:
docker pull IMAGE_ID
- Downloads the specified docker image.
docker run -p 49162:80 -d IMAGE_ID
- Starts a new container instance with the specified image id. The id can be found with
docker image ls
. - The
-p
parameter can be optionally used to changed the default port of the container. - The
-d
option instructs the container to run in the background. - alternative to
-d
, you can use the-it
option which tells Docker to create a bash shell in the container, so that we can directly interact with it.
- Starts a new container instance with the specified image id. The id can be found with
docker pause CONTAINER_NAME
- Pauses a container and suspends all processes.
- use
docker unpause CONTAINER_NAME
for unsuspending all processes of the container.
docker stop CONTAINER_NAME
- a termination signal is sent to the container.
docker restart CONTAINER_NAME
- firsts stops and then starts the container.
docker exec -it CONTAINER_ID "bash"
- Starts an interactive bash shell inside your running container.
- To quit the bash type exit. The container continues running.
docker exec -it sql1 mkdir /folder1/folder2/folder3
- Creates a new directory (3 folders) inside the container.
docker stop CONTAINER_ID
- Stops a container with the given id.
- You could also use the name of the container instead of the container id.
docker ps
- Lists all running containers in the current machine.
- With the
-a
parameter you can list all containers (running and not running). - Alternative you can use the
docker container ls
command which also lists all running containers.
docker image ls
- Lists all, not deleted, images we have created.
- It is possible that an image has multiple tags. We can find out if two images are the same by checking their Image ID.
docker build -t aRandomName:aRandomVersion .
- builds a new image with the given name and a version (optional).
- The command has to be executed inside a folder which contains the Dockerfile file.
- The
.
is the path where the local files are being sent to when we create the new image. - Use the –no-cache option if you want to build the image from scratch.
- The
-f
parameter can be used to define the name of a specific Dockerfile.
docker tag localImageName pathToRemoteRegistry/remoteImageName
- Generates a tag for a remote image.
docker push pathToRemoteRegistry/remoteImageName
- Pushes an image to a remote registry.
docker start CONTAINER_ID
- Starts a new container instance with the specified container id. The id can be found with
docker ps -a
.
- Starts a new container instance with the specified container id. The id can be found with
docker rm CONTAINER_ID
- Removes the container. You can check that the container is gone by typing
docker ps -a
. - You can also use the name of the container instead of the container id.
- Removes the container. You can check that the container is gone by typing
docker image rm -f IMAGE_ID
:- Removes one ore more container images with the given id. The id can be found with the
docker image ls
ordocker images
command. - You can delete more than one image with one command. Simply add the IDs one after the other.
- The
-f
parameter is for force. - We can also use the
docker rmi IMAGE_NAME
command.
- Removes one ore more container images with the given id. The id can be found with the
docker container inspect CONTAINER_ID
- Returns a JSON object with the complete configuration of the container.
docker builder prune
- Removes the build cache, resulting into build-steps to be created again the first time an image is build.
docker container prune
- Removes all containers (both running and not running) from Docker. Very helpful command when you want to start with fresh instances of your Docker Images. Use it with caution ;).
docker attach CONTAINER_ID
- Attach to a running container.
docker cp foo.txt CONTAINER_NAME:/foo.txt
- Copy one local file into a running container.
docker cp CONTAINER_NAME:/foo.txt foo.txt
- Copy a file from a running container into your computer.
docker cp src/. CONTAINER_NAME:/target
- Copy multiple files into a running container.
docker cp CONTAINER_NAME:/src/. target
- Copy multiple files from a running container into your computer.
docker log CONTAINER_ID
- Logged information, warnings, errors to troubleshoot problems in your Container.
docker volume create
- Creates a volume which is a specific folder location available to the container for storing persistent data.
docker stats
- Get information about the container like CPU and memory usage.
docker-compose up
- The “easy” way to start and orchestrate a collection of containers. For more complex orchestration-scenarios check Kubernetes.
- With the
--no-start
option, you can block the starting of the Containers defined in the docker-compose file. This option is very helpful when you want to do some bootstrapping actions before letting the Containers start.
docker-compose down
- Stops all running containers started from
--docker-compose up
. - Removes also the data stored in the Database Containers.
- Stops all running containers started from
For a more detailed documentation of the previous commands and their available option visit the Docker documentation.