How to deploy a simple c/c++ program using docker

How to deploy a simple c/c++ program using docker

1. First, create a hello-world.cpp file

The program code is as follows:

#include <iostream>
using namespace std;

int main () {
	cout << "hello-world" << endl;
	cout << "my first docker" << endl;
	return 0;
}

For example, I created a DockerFile/hello-world directory in my home path and created a hello-world.cpp source file in it.

insert image description here

2. Write DockerFile file

This file and the source file are in a folder named hello-dockerfile as shown above.
First give the file content, and then explain it one by one:

FROM codenvy/cpp_gcc 

RUN mkdir /home/user/myapp

ADD hello-world.cpp /home/user/myapp

WORKDIR /home/user/myapp

RUN g++ hello-world.cpp -o hello

CMD ["./hello"]

The commands for Dockerfile can be found in another blog of mine: https://www.jb51.net/article/197996.htm

1. FROM codenvy/cpp_gcc

FROM is followed by the base image used by the target image. There can be only one base image. You can use the docker search command to find the base image you want on the docker Hub . Since we want to compile a C++ file, we can use an image with g++ or gcc as the base image. We can search for an image with g++, as shown below:

insert image description here

For example, I use the second one, and then pull the image to the local through the docker pull command, as shown in the following figure

insert image description here

Enter the docker images command to view all local images, as shown in the following figure:

insert image description here

For example, I have two images here: one is the codenvy/cpp_gcc that I just downloaded, and the other is the ubuntu image.

Some people may wonder here, why not just use the Ubuntu image? In our impression, Ubuntu comes with g++ and gcc compilers, and this image is relatively small. Let me explain here: Since the ubuntu image under docker only retains things related to the ubuntu kernel, everything outside the core has been deleted as much as possible. This also explains why the ubuntu image we usually install is nearly two GB, while the ubuntu image of docker is only more than seventy MB. There are no gcc and g++ commands in this Ubuntu image, not even sudo commands, so you need to download and configure them manually.

For example, we use the ubuntu image to generate a container:

insert image description here

Then check the version of g++:

insert image description here

Found that g++ was not installed

insert image description here

Neither does the sudo command.
So we need to download an image with g++, or we can install it manually in the Ubuntu image, but it is a bit troublesome.

2. RUN mkdir /home/user/myapp

The RUN command is required when building a container, followed by our common Linux commands.

This command creates a new myapp folder in the /home/user path.

3. ADD hello-world.cpp /home/user/myapp

The ADD command copies files from the host machine to the image. You can also use the COPY command, but ADD is more powerful than COPY . ADD has a decompression function.

This command copies the hello-world.cpp source file created in the first step to the /home/user/myapp path in the image.

4. WORKDIR /home/user/myapp

The WORKDIR command specifies the default working directory of the container when creating the container.

This command uses the home/user/myapp path in the image as the default working path.

5. RUN g++ hello-world.cpp -o hello

This command uses g++ to compile the hello-world.cpp source file and build an executable file hello

6. CMD ["./hello"]

CMD is the first command to be run after the specified container is created.

This command runs the executable file hello built in the previous step

3. Create an image

Use command:

docker build -f ./hello-dockerfile -t my_hello_docker:v1.0 .

./hello-dockerfile is the location of our Dockerfile
my_hello_docker is the name of the newly created image
v1.0 is the tag of the image, for example, this is the first version

The process of building an image is shown in the following figure:

insert image description here

After the build is complete, we check whether the build is successful and view all the current local images through the docker images command:

insert image description here

Here we see one more image: my_hello_docker , which is the image we just created, indicating that the image was built successfully.

4. Run a container through the image

Instantiate a container with the following command

docker run my_hello_docker:v1.0

Then you will get the following output:

insert image description here

The output is consistent with the C++ file we wrote, indicating that Docker was successfully used to deploy a simple C/C++ program and achieve the goal.

This is the end of this article about how to use docker to deploy a simple c/c++ program. For more information about how to deploy c/c++ programs with docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of how to use Docker to deploy a web project and package it into an image file
  • Detailed steps to deploy tomcat and java applications in docker
  • Tutorial to start deploying Python applications on Docker
  • Methods for deploying MySQL services in Docker and the pitfalls encountered
  • Tutorial on deploying Python's Flask framework on Docker
  • Detailed explanation of Docker automatic deployment of tomcat
  • Detailed explanation of how to set up Go and deploy applications in Docker
  • Detailed explanation of CentOS7 Docker Nginx deployment and operation
  • Tutorial on deploying Docker on multiple hosts using OpenVSwitch

<<:  MySQL export of entire or single table data

>>:  Vue implements small form validation function

Recommend

VMware15/16 Detailed steps to unlock VMware and install MacOS

VMware version: VMware-workstation-full-16 VMware...

Vue implements video upload function

This article example shares the specific code of ...

JavaScript to achieve a simple magnifying glass effect

There is a picture in a big box. When you put the...

Introduction to Spark and comparison with Hadoop

Table of contents 1. Spark vs. Hadoop 1.1 Disadva...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

MySQL column to row conversion tips (share)

Preface: Because many business tables use design ...

mysql update case update field value is not fixed operation

When processing batch updates of certain data, if...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

Detailed explanation of json file writing format

Table of contents What is JSON Why this technolog...

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

MySQL series 9 MySQL query cache and index

Table of contents Tutorial Series 1. MySQL Archit...

How to quickly insert 10 million records into MySQL

I heard that there is an interview question: How ...

Why is it not recommended to use index as the key attribute value in Vue?

Table of contents Preface The role of key The rol...

Complete steps to implement location punch-in using MySQL spatial functions

Preface The project requirement is to determine w...