...

/

Building and Running a Docker Container from a Dockerfile

Building and Running a Docker Container from a Dockerfile

Learn to write a Dockerfile to build a container for the app and run it.

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 ...