1. What is Docker?
Docker is an open source container engine. It packages an application and its dependencies into a container. The container runs on many Linux machines.
Full lesson: Docker tutorial
45 questions about Docker with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.
Docker is an open source container engine. It packages an application and its dependencies into a container. The container runs on many Linux machines.
Full lesson: Docker tutorial
A container is a light unit that runs one application. Containers share the host kernel. They stay isolated from each other.
Full lesson: Docker intro
An image is a read-only template for a container. Docker builds a container from an image. Docker pulls the image from Docker Hub when it is not local.
Full lesson: Docker image usage
Run the docker images command. The output shows the repository, tag, image ID, creation time, and size.
docker images
Full lesson: Docker images command
Run the docker ps command. It shows only running containers by default. Add the -a option to show stopped containers too.
docker ps -a
Full lesson: Docker ps command
Use the docker run command. It creates and starts a new container. Add -it to open an interactive shell.
docker run -it ubuntu:15.10 /bin/bash
Full lesson: Docker run command
A Dockerfile is a text file with build instructions. Docker reads it to build a custom image. Each line adds a step to the image.
FROM nginx
RUN echo 'hello' > /usr/share/nginx/html/index.html
Full lesson: Docker dockerfile
Use the docker cp command. Put the container name and path before the colon. Put the host path after the colon.
docker cp my_container:/path/in/container /path/on/host
Full lesson: Docker cp command
Use the docker exec command. It starts a new process in the container. The container must be running.
docker exec -it my_container /bin/bash
Full lesson: Docker exec command
Run the docker diff command. The output marks added files with A. It marks deleted files with D and changed files with C.
docker diff CONTAINER
Full lesson: Docker diff command
Use the docker commit command. It saves the current state of the container as an image. Add -m to write a commit message.
docker commit my_container my_new_image
Full lesson: Docker commit command
Use the docker tag command. It creates an alias for the image. The alias can use an easy name or a version number.
docker tag myimage:1.0 myrepo/myimage:latest
Full lesson: Docker tag command
Run the docker rm command with the container name or ID. The command does not delete a running container. Stop the container first.
docker rm my_container
Full lesson: Docker rm command
Use the docker rmi command with the image name or ID. Add -f to force the delete. The command frees local storage space.
docker rmi my_image
Full lesson: Docker rmi command
The docker rm command fails on a running container. This is a common mistake. Stop the container first, then run docker rm again.
docker stop my_container
docker rm my_container
Full lesson: Docker rm command
The docker pull command downloads an image from a registry to your local machine. You can specify a tag to get a specific version.
docker pull ubuntu:20.04
Full lesson: Docker pull command
Use the docker logs command to see the output of a container. Add the -f option to follow the log in real time.
docker logs -f my_container
Full lesson: Docker logs command
The docker port command shows how container ports map to host ports. You can also specify a private port to see one mapping.
docker port my_container
Full lesson: Docker port command
Use the docker top command to list processes in a container. It works like the Linux ps command.
docker top my_container
Full lesson: Docker top command
The docker info command displays system-wide information. It includes the number of containers and images, and the storage driver.
docker info
Full lesson: Docker info command
Use the docker load command with the -i option to load an image from a tar archive. The image becomes available locally.
docker load -i myimage.tar
Full lesson: Docker load command
The docker push command uploads a local image to a registry. You must tag the image with the registry name first.
docker push myrepo/myimage:latest
Full lesson: Docker push command
The docker kill command sends a SIGKILL signal to the container. The container stops without a graceful shutdown.
docker kill my_container
Full lesson: Docker kill command
Pull the official Node.js image from Docker Hub. Then run a container from that image.
docker pull node:latest
Full lesson: Docker install node
Pull the official PHP image from Docker Hub. You can choose a specific version tag.
docker pull php:5.6-fpm
Full lesson: Docker install php
Docker prevents the removal by default. You must stop and remove the container first, or use the -f option to force removal.
docker rmi -f my_image
Full lesson: Docker rmi command
The container runs in the foreground and attaches to your terminal. You cannot use the terminal until the container stops.
docker run -d my_image
Full lesson: Docker run command
The push fails because the image name does not include a registry or repository. You must tag the image with the correct name first.
docker tag myimage:1.0 myrepo/myimage:latest
Full lesson: Docker tag command
The command fails because docker exec requires a running container. Start the container first.
docker start my_container
Full lesson: Docker exec command
The load command fails because it expects a tar file created by docker save. Make sure the file is a valid image archive.
docker load -i myimage.tar
Full lesson: Docker load command
Docker has three main parts. The client sends commands. The daemon builds and runs containers. Images and containers are the core objects.
Full lesson: Docker intro
Install Docker first. Then run a test container. The tutorial shows the first steps.
docker run hello-world
Full lesson: Docker tutorial
Add the tag after a colon. The default tag is latest. This command gets the 20.04 tag of ubuntu.
docker pull ubuntu:20.04
Full lesson: Docker pull command
Use docker run with the image name. Docker starts a new container from that image. Add -d to run it in the background.
docker run -d nginx
Full lesson: Docker run command
Use the docker logs command with the container name. Add -f to follow new output. Add --tail 10 to show only the last 10 lines.
docker logs -f --tail 10 my_container
Full lesson: Docker logs command
Use docker port with the container name. Docker prints the host port for each container port. You can also ask for one private port.
docker port my_container 80
Full lesson: Docker port command
Use the docker top command with the container name. Docker shows the processes that run in that container. This helps you check the main process.
docker top my_container
Full lesson: Docker top command
Use the docker info command. Docker prints the version, storage driver, and container counts. This helps you check the daemon state.
docker info
Full lesson: Docker info command
Use docker load with the -i option and the file name. Docker reads the tar file and adds the image to the local store. The file must be a tar archive.
docker load -i myimage.tar
Full lesson: Docker load command
Use docker push with the full image name. The name must include the registry and the tag. Log in first if the registry needs a password.
docker push myrepo/myimage:1.0
Full lesson: Docker push command
The command fails because it needs a container name or ID. Docker cannot guess the target. Always give the container name.
docker logs my_container
Full lesson: Docker logs command
The command prints no mapping. The container has no host port. Start the container with -p to publish a port.
docker run -d -p 8080:80 nginx
Full lesson: Docker port command
The command fails because the container is not running. There are no processes to list. Start the container first.
docker start my_container
Full lesson: Docker top command
Docker sends the image to Docker Hub by default. The push can fail if you are not logged in. Use the full registry name to target the right registry.
docker push docker.io/myrepo/myimage:1.0
Full lesson: Docker push command
The command fails with an error. Docker needs a tar archive from docker save. Check the file type before you load it.
docker load -i myimage.tar
Full lesson: Docker load command