/

Getting Started with Docker: First Steps After Installation

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.

Docker First Steps

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:

  1. Open the macOS Terminal.
  2. Navigate to your desired directory (e.g., cd dev) and create a subdirectory called docker to host your Docker experiments (mkdir docker and cd docker).
  3. 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

Clone Repository

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Install the base requirements for the app.
# This stage is to support development.
FROM python:alpine AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

# Run tests to validate the app
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

# Clear out the node_modules and create the zip
FROM app-base AS app-zip-creator
RUN rm -rf node_modules && \
apk add zip && \
zip -r /app.zip /app

# Dev-ready container - actual files will be mounted in
FROM base AS dev
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]

# Do the actual build of the mkdocs site
FROM base AS build
COPY . .
RUN mkdocs build

# Extract the static content from the build
# and use an nginx image to serve the content
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:

Download Base Image

Then it executes all the commands defined in the Dockerfile:

Build Commands

It continues until it reaches the end:

Build Complete

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:

Docker Run Help

Executing the docker run command will give you the container ID:

Container ID

Now you are up and running with Docker, ready to explore and experiment further!

tags: [“Docker”, “containers”, “images”, “Dockerfile”, “Docker installation”]