Getting Started with Docker: First Steps After Installation
Once you have completed the Docker installation on your system, you’re ready to take your first steps in creating and running Docker images and containers. This guide will walk you through the process.
To begin, you can either run the commands directly in the built-in terminal or use your own shell. Here’s how I prefer to do it on macOS:
- Open the macOS Terminal.
- Navigate to your desired directory (e.g.,
cd dev
) and create a subdirectory calleddocker
to host your Docker experiments (mkdir docker
andcd docker
). - Clone the repository https://github.com/docker/getting-started into a folder called
getting-started
by running the command:1
git clone https://github.com/docker/getting-started
Next, navigate into the getting-started
folder and run the following command to build your image:
1 | docker build -t docker101tutorial . |
This command builds the image using the contents of the current folder and gives it the tag name docker101tutorial
.
The Dockerfile used in this process looks like this:
1 | # Install the base requirements for the app. |
This Dockerfile uses three base images: python:alpine
, node:12-alpine
, and nginx:alpine
.
When you run docker build -t docker101tutorial .
, Docker starts by downloading the first base image:
Then it executes all the commands defined in the Dockerfile:
It continues until it reaches the end:
Now that you have the docker101tutorial
image, you can run a container based on it.
Execute the following command to run the container:
1 | docker run -d -p 80:80 --name docker-tutorial docker101tutorial |
In this command, we use the -d
option to run the container in the background and print the container ID. The -p
option maps port 80 of the container to port 80 on the host machine. The --name
option assigns a name to the container, and finally, we specify the image name (docker101tutorial
) to create the container.
If you have any doubts about a command option, you can run docker <command> --help
. For example, docker run --help
will provide detailed explanations:
Executing the docker run
command will give you the container ID:
Now you are up and running with Docker, ready to explore and experiment further!
tags: [“Docker”, “containers”, “images”, “Dockerfile”, “Docker installation”]