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:
git clone https://github.com/docker/getting-started
Next, navigate into the getting-started
folder and run the following command to build your image:
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:
FROM python:alpine AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM node:12-alpine AS app-base
WORKDIR /app
COPY app/package.json app/yarn.lock ./
RUN yarn install
COPY app/spec ./spec
COPY app/src ./src
RUN yarn test
FROM app-base AS app-zip-creator
RUN rm -rf node_modules && \
apk add zip && \
zip -r /app.zip /app
FROM base AS dev
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]
FROM base AS build
COPY . .
RUN mkdocs build
FROM nginx:alpine
COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip
COPY --from=build /app/site /usr/share/nginx/html
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:
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!