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

Detailed explanation of tcpdump command examples in Linux

Preface To put it simply, tcpdump is a packet ana...

Using NTP for Time Synchronization in Ubuntu

NTP is a TCP/IP protocol for synchronizing time o...

WeChat Mini Program video barrage position random

This article shares the specific code for randomi...

Summary of Mysql high performance optimization skills

Database Command Specification All database objec...

Vue multi-page configuration details

Table of contents 1. The difference between multi...

Linux command line operation Baidu cloud upload and download files

Table of contents 0. Background 1. Installation 2...

Implementing the preview function of multiple image uploads based on HTML

I recently wrote a script for uploading multiple ...

In-depth analysis of Vue's responsive principle and bidirectional data

Understanding object.defineProperty to achieve re...

...

Modify the boot time of grub in ubuntu

The online search to modify the grub startup time...

Example of how to configure multiple virtual hosts in nginx

It is very convenient to configure virtual host v...

10 skills that make front-end developers worth millions

The skills that front-end developers need to mast...

25 Tools to Improve Website Usability and Conversion Rates

For a website, usability refers to whether users c...

How to make React components full screen

introduce This article is based on React + antd t...