Docker

Installation

Please visit the official documentation page

but do not forget to add the permission problem:

Docker image

If the image doesn’t exist it will pull it from hub.docker.com

Basically, it runs the pull command beforehand:

for instance, to install Ubuntu 20.04

to check the version, just run:

To see available images on your machine:

or

You can search directly from the terminal for images:

And you can type exit to leave.

To remove all dangling images:

to remove all unused images, not just dangling ones

Docker container

The run command will start a container from a given image:

-i: means interactive
-t: means tty

If you don’t provide a container name with the --name, then the daemon generates a random string name for your container, such as “loving_tereshkova”.

To spin up a container from an image and delete it automatically after exit:

you can give your container a name and join it via another terminal

or you can resume it again and reuse it:

To see the running containers:

To see all running containers:

The following command has the same output as docker ps, but it is a more updated command to it’s better to use this one

To list all containers (default shows just running)

To terminate a container:

“kill” will terminate the container immediately.

Next time you want to run your container, you can use the name you gave to it :

Now, if we install some packages inside our container, we can use docker diff to the changes we made to our container:

to find where the images and dockers are stored:

which on Ubuntu they are under:

to remove a container:

Remove all containers:

To remove all the unused containers at once:

to remove an image:

if you have a container that has been spun from an image, you need to delete it before removing an image, or you can use the force option:

to list dangling images:

to remove dangling images:

Creating Images

We can use the following to build an image from our container and make a new image:

  • commit
  • build

1. Creating images from an existing container changes

commit will create an image from your container:

2. Build an image

What build does is, pulling images, running an intermediate container from them, run your commands on them, and finally commit that and build an image, so it is basically an automated commit.

Example 1

First, search in the hub.docker.com for an image i.e. “Ubuntu” to find the right name then create a file and rename it “Dockerfile” and add the following to that:

Now you can build your custom image:

The default file that docker looks for is Dockerfile without any extension. After building your image you can  a container from that with:

Transferring Files From/Into Containers

We can also use volumes.

Volume

If you delete your container, your data stored on the container will be lost. You can use volume to store your data outside of containers. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

We can map a directory on the host machine into a directory into the docker. This is called volume. If you delete the container, the volume won’t be deleted. To get information about the command, just run:

to get a list of existing volumes:

to display detailed information on a volume

attach a volume to a container

Cleaning build cache

Developing docker with GUI (X Window System Forwarding)

Allow the Docker container to access your X11 server using xhost:

create our container:

In this command:

  • -v /tmp/.X11-unix:/tmp/.X11-unix : Allows the container to use the host’s X11 server for GUI applications.
  • -e DISPLAY=$DISPLAY: Sets the DISPLAY environment variable inside the container to use the host’s display.
  • --network=host: Uses the host’s network stack inside the container. This is useful for applications that require access to the host’s network.

Now inside the container, install the xeyes

Now, if run xeyes on the docker, it will be forwarded to the host:

Docker Entry point

Docker’s entry point specifies the executable to be run when a container starts. It is often used in conjunction with the CMD instruction to set a default command and its arguments. The main difference between CENTRYPOINTand CMDis that ENTRYPOINTconfigures a container to run as an executable, whereas CMD sets default commands and arguments that can be overwritten from the command line when the container is run.

Understanding ENTRYPOINT

– Executable Form:  This is the preferred form for ENTRYPOINT because it allows the entry point to receive arguments from the CMD instruction or from the command line when running the container. It’s defined as a JSON array with the executable at the first position and arguments in the subsequent positions: ENTRYPOINT ["executable", "param1", "param2"] .

– Shell Form: This form runs the entry point command within a shell, which allows you to use shell processing on the given command. It’s defined as: command param1 param2 . However, it doesn’t allow the appending of additional arguments from the CMD instruction or the command line.

Example with ENTRYPOINT

Let’s say you have a Python application that you want to run inside a Docker container. The application is a simple script that prints a message, and you want to be able to pass the message as an argument when running the container.

1. Python Script (app.py):

2. Dockerfile:

3. Building and Running the Docker Container:
– To build the Docker image, run: docker build -t my-python-app .
– To run the container with the default message, simply execute: docker run my-python-app
– To run the container with a custom message, append the message at the end of the run command: docker run my-python-app "Hello from Docker!"

In this example, ENTRYPOINT is set to ["python", "app.py"], making the Python interpreter with app.py as its argument the default executable for the container. When you run the container without any additional arguments, it prints “Hello, World!”. If you provide an argument, like "Hello from Docker!", it overrides the default CMD (if it were specified) but gets passed to the ENTRYPOINT as additional arguments, allowing the script to print the custom message.

Developing your project on Docker

I have used VS Code for that purpose, first install the following extensions:

  1. Remote Containers
  2. Remote explorer
  3. Docker Explorer

Now run your container and in the VS Code click on the remote explorer:

Now right click on the running container and click on attach to container:

if this is the first time that you are doing this, it will initialize a new instance of VSCode, now in the new VSCode, at the bottom, you can see the connected container:

 

Now you should reinstall your extension such as C/C++ CMake, CMake Tools, CMake Integration in the  container:

Now press ctrl+shit+p to open the pallet windows, and type: cmconf

and now by clicking on the CMake icon on the left, we build/ debug our applications

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x