Skip to content

Instantly share code, notes, and snippets.

@jpret
Created September 2, 2021 08:15
Show Gist options
  • Select an option

  • Save jpret/b0e13613f3cb4c8fdc6af121a32840d8 to your computer and use it in GitHub Desktop.

Select an option

Save jpret/b0e13613f3cb4c8fdc6af121a32840d8 to your computer and use it in GitHub Desktop.
Docker C++ Example

Readme Docker

Docker containers are used to run applications in closed containers.

The Dockerfile describes the environment prerequisites for the specific application.

The Dockerfile can then be used to create an image. This image can then be executed in a container(s).

Install docker

  1. Go to Docker and download the necessary installation file.
  2. Windows 10: during the installation you will be prompted to install the Windows Subsystem for Linux (WSL) as well. Follow the instructions during the installation process.

Dockerfile example

After installation of Docker you can use the tutorial as part of the installation process to get started.

# Pull in our OS
FROM alpine

# Install dependencies (in this case c++ compiler)
RUN apk add g++

# Set working directory
WORKDIR /example

# Copy example to container (into working directory)
COPY main.cpp .

# Compile the example
RUN g++ main.cpp -o example

# Command executed on startup of the container 
# There may only be one CMD in a dockerfile
CMD ./example

Build an image

After the Dockerfile is created, one can create an image with the following command:

# docker build -t <image_name> <folder with Dockerfile>
docker build -t example .

Startup a container

After an image has been built, start a container, based on an image:

# docker run --name <name of container> -t <name of docker image>
docker run --name example_container -t example

Startup an existing container

If the container has been created and you want to restart the container use:

# Use -i to attach stdin to the terminal
# docker start -i <name of container>
docker start -i example-container
#include <iostream>
int main (int argc, char *argv[]) {
std::cout << "Hello World\n" << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment