This Docker Tutorial should help you get started with Docker.
- How to get a running Docker Machine on your computer
- How to create and manage Docker Images
- How to create and manage Docker containers
- How to delete Docker images and containers
- How to login to Docker Hub
- How to publish images to Docker Hub
You’ll learn how to start a Docker Machine on your PC, how to create images, how to create containers, and to cleanup your Docker Machine, login to Docker Hub, tag and publish images.
1. How to get a running Docker Machine on your computer
First thing you need to do before starting to manage images and containers is to have a Docker Machine up and running. For this tutorial I’m using Virtual Box for running the Docker Machine.
The next command creates a Docker Machine named dev. The command will check and download the required dependencies, create the VM, create a SSH key, start the VM and assign an IP and generate certificates.
docker-machine create --driver virtualbox dev
Running the above command is only needed the first time you create a Docker Machine. When you just need to start it you must run:
docker-machine start dev
After you have the Docker Machine running, you’ll be able to see the PORT and IP by running the next command. The printed result also contains a short command that you must run in order to set Docker Machine configuration in your environment.
docker-machine env dev
You must now set the environment with the Docker Machine configuration for the current terminal session by running:
eval $(docker-machine env dev)
That’s all for the first step. Starting a Docker Machine is quite easy.
In case you’re getting an IP allocation error, check out this article. It might help.
2. How to create and manage Docker Images
Now that we have the machine up and running we can list all the available images with:
In order to create a new image we must have a Dockerfile. I won’t get into how to create Dockerfile in this article but I’ll continue the tutorial using a base Nginx Dockerfile. Place this content in a file named Dockerfile
RUN nginx -v
CMD ["bash", "-c", "nginx -g 'daemon off;'"]
Now you have a base Dockerfile and you can create an image from it by running:
docker build -t the_tag .
This command also requires that you have the Dockerfile in cwd. The dot from the end specifies that you are using a Dockerfile from the local directory. You can also build using a Dockerfile that’s not in the cwd by specifying the path to the Dockerfile:
docker build -f Dockerfile -t the_tag .
For both of the above commands you’ll see the following steps being executed:
Sending build context to Docker daemon 338.1MB
Step 1/4 : FROM nginx:latest
Step 2/4 : RUN nginx -v
---> Using cache
Step 3/4 : EXPOSE 80
---> Using cache
Step 4/4 : CMD ["bash", "-c", "nginx -g 'daemon off;'"]
---> Using cache
Successfully built c3f3bfb92c45
Successfully tagged the_tag:latest
If something goes wrong in any step the build will fail and the error will be printed. If you now run docker images again you’ll see:
REPOSITORY TAG IMAGE ID CREATED SIZE
the_tag latest c3f3bfb92c45 7 minutes ago 109MB
3. How to create and manage Docker containers
We now have a Docker image created and we’re ready to create and manage containers. You can list all available containers by running:
Create a new container using the image we built by running:
docker run -d --name docker-nginx -p 80:80 the_tag
-d flag causes the container to run in detached mode
–name allows you to easily identify the container by setting a label
-p allows you to set publish the HOST:CONTAINER ports.
If you now run docker ps you’ll see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2e1bdcf12a0 image_tag "bash -c" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp docker-nginx
You may now stop and delete this container by running:
docker stop c2e1bdcf12a0
docker rm c2e1bdcf12a0
4. How to delete Docker images and containers
In some articles I previously wrote I described how to keep your Docker Machine clean and how to delete certain images and containers or do a full cleanup.
Check out this article if you’re on OSX/Linux.
Check out this article if you’re using Windows.
5. How to login to Docker Hub
If you plan to publish your images to Docker Hub you’ll probably need to login.
docker login --username firstname.lastname@example.org --password THE_PASSWORD docker-hub.example.com:PORT
Be careful to specify the PORT. If you don’t, the authentication will work but publishing images will not find a token for the host.
6. How to publish images to Docker Hub
You’re now have an image and are authenticated to your Docker Hub. You can publish the image and make it available for others to use as well.
docker images # prints the hash
docker tag HASH docker-hub.example.com:443/<IMAGE_REPOSITORY_NAME>:0.0.1
docker push docker-hub.example.com:443/<IMAGE_REPOSITORY_NAME>
That’s it. The image is now on the hub.