0

I started to experiment with the docker but have some questions regarding how to develop on it and regarding its use cases. If anyone could guide me through these questions, it will be much appreciated.

First, As far as I understood, docker is used mainly for developing applications on custom environments, thus avoiding the tidious installation processes. This is initially my intention, why I'd like to use docker for.

I've created a docker file which builds successfuly, and which has basic C++ development tools based upon library/gcc. I want to be able to develop in this docker container as you would do on your terminal.

What I did is I created a docker image from a Dockerfile. (I can observe that it is successfully created)

docker build -t mydockerimage .

Then run the docker in detached mode.

docker run -d mydockerimage

At this point, I am notified with the ID of the docker container. However docker container does not seem to be running when I check the output of:

docker container ls

Here comes the first question, why is my docker container not running?

To my understanding, simplest way to interact with the docker container is as follows:

docker exec -it <container_id_or_name> echo "Hello from container!"

Is this true? Is this a use case of docker in which I simply can start the container and exec some Linux command on it?

Moreover, I get a permission denied on /var/lib/docker.sock when I try to execute docker commands without sudo. What am I missing here?

Thank you in advance.

mozcelikors
  • 2,582
  • 8
  • 43
  • 77
  • i suggest you go or google `docker 101`, in docker container something has to be running otherwise the container stop, so i you do `docker run -it mydockerimage bash` it will start bash in that container and take your terminal there ... – Mazel Tov Mar 19 '18 at 14:11

1 Answers1

0

Do you provide an entrypoint or CMD in your dockerfile? This will be executed inside your container and keeps the container running. You can find some details here.

In short. Docker has a default entrypoint: /bin/sh -c, but no default CMD. Check the dockerfile of ubuntu. This has bash as CMD so it's executing /bin/sh -c bash.

$ docker run -it ubuntu bash 
root@9855e779cab2:/#

This will result in an interactive shell in which you can execute commands like on an ubuntu. If you exit the container the container will stop running.

To keep a container running you can use the -d option. It will run the container in the background as a daemon:

$ docker run -d -it ubuntu bash
2606ad8e095baa0237cc30e599a26a4d727d99d47392d779fb83cd50f1a39614
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2606ad8e095b        ubuntu              "bash"              18 seconds ago      Up 17 seconds                           cranky_johnson

Now you can exec inside the container to "go inside" the container and execute ubuntu commands.

$ docker exec -it 2606ad8e095b bash
root@2606ad8e095b:/#

When you exit the container it remains running in the background. Now we can execute your command too:

$ docker exec -it 2606ad8e095b echo "Hello from container!"
Hello from container!

This will open a bash session in your container and echo the string.

I think it's important in your case you define some entrypoint (which can also be a script) or a CMD. Probably you need something very similar to Ubuntu when you just want to use bash inside your container.

Moreover, I get a permission denied on /var/lib/docker.sock when I try to execute docker commands without sudo. What am I missing here?

This is normal. The Docker daemon currently requires root privileges. So you have to use docker with your root user or users which have root priviledges and you have to add sudo every time. You can add your user to a docker group. Every time the daemon starts, it makes the ownership of the Unix socket read/writable by the docker group. This means you can use docker without using sudo everytime when that user is inside your docker group.

To add your user to the docker group:

$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ exit
ssh back or open new shell
lvthillo
  • 28,263
  • 13
  • 94
  • 127