Introduction
Introduction
This guide illustrates various ways that you can improve how you build the application with Docker.
The application
The example project for this guide is a client-server application for translating messages to a fictional language.
Here’s an overview of the files included in the project:
The cmd/
directory contains the code for the two application components: client and server. The client is a user interface for writing, sending, and receiving messages. The server receives messages from clients, translates them, and sends them back to the client.
The Dockerfile
A Dockerfile is a text document in which you define the build steps for your application. You write the Dockerfile in a domain-specific language, called the Dockerfile syntax.
Here’s the Dockerfile used as the starting point for this guide:
Here’s what this Dockerfile does:
# syntax=docker/dockerfile:1
FROM golang:1.20-alpine
The
FROM
instruction uses version1.20-alpine
of thegolang
official image.WORKDIR /src
Creates the
/src
working directory inside the container.COPY . .
Copies the files in the build context to the working directory in the container.
RUN go mod download
Downloads the necessary Go modules to the container. Go modules is the dependency management tool for the Go programming language, similar to
npm install
for JavaScript, orpip install
for Python.RUN go build -o /bin/client ./cmd/client
Builds the
client
binary, used to send messages to be translated, into the/bin
directory.RUN go build -o /bin/server ./cmd/server
Builds the
server
binary, which listens for client translation requests, into the/bin
directory.ENTRYPOINT [ "/bin/server" ]
Specifies a command to run when the container starts. Starts the server process.
Build the image
To build an image using a Dockerfile, you use the docker
command-line tool. The command for building an image is docker build
.
Run the following command to build the image.
This creates an image with the tag buildme
. An image tag is the name of the image.
Run the container
The image you just built contains two binaries, one for the server and one for the client. To see the translation service in action, run a container that hosts the server component, and then run another container that invokes the client.
To run a container, you use the docker run
command.
Run a container from the image in detached mode.
This starts a container named
buildme
.Run a new command in the
buildme
container that invokes the client binary.
The docker exec
command opens a terminal user interface where you can submit messages for the backend (server) process to translate.
When you’re done testing, you can stop the container:
Summary
This section gave you an overview of the example application used in this guide, an introduction to Dockerfiles and building. You’ve successfully built a container image and created a container from it.
Last updated