Search⌘ K
AI Features

Building and Running a Docker Container from a Dockerfile

Explore the process of building a Docker image from a Dockerfile, running a container with correct port mapping, and managing your app through Docker commands. Understand how to expose your Go web app for browser access and verify running containers.

We have created the following Dockerfile to build and run a Docker image for our app:

FROM golang:1.18.4
WORKDIR /app
COPY . .
RUN go mod tidy
RUN go build -o coffeeshop .
CMD ["./coffeeshop"]
Dockerfile for a Go app

Now, let's see how we can build it.

Installing Docker

Before doing anything, we will first need to install Docker Engine on our system. Please refer to the Setting up Docker and Docker Compose lesson in the appendix to see how.

Build the Docker image

We can use the docker build command, which can use a Dockerfile, to create a Docker image along with a build context. Here, by context we mean all the files that we're adding to the image in line 4 ...