Difference between docker exec and docker container exec [duplicate] - docker

I noticed in the latest Docker CLI documentation that Docker CLI command list has expanded.
If I used docker exec earlier to start executable inside container now I can also use docker container exec command.
docker container run command is similar to docker run, etc.
So which commands are preferrable now? Old syntax or new docker container syntax? Unfortunately I couldn't find any explanation in the docs.
Also what is the difference between docker container run and docker container create commands? And between docker container stop and docker container kill? The description and syntax are very similar.
Thanks.

As docker grew in features over time and new commands were added cli needed some redesign. You should use docker container exec to be compatible in the future, but docker exec is in fact an alias so until someone decided to deprecate it should also work. If you are interested, you can start reading about this change from this PR: https://github.com/moby/moby/pull/26025

Related

How to access to a created docker image

I see that several varieties of this question have been asked in StackOverflow, but unfortunately, I could not get help from any of them.
I have created a docker container with the following command within an Ubuntu operating system:
docker run --name hasan -it ubuntu:latest bash
Inside the docker image, I set up a new file system. Now, I would like to reach the same container to continue to setup my filesystem inside.
How I can do it?
Thanks,
Access the running container with:
docker exec -it hasan bash
You might want to do the setup within a Dockerfile instead of doing it manually.

docker exec or docker container exec

I noticed in the latest Docker CLI documentation that Docker CLI command list has expanded.
If I used docker exec earlier to start executable inside container now I can also use docker container exec command.
docker container run command is similar to docker run, etc.
So which commands are preferrable now? Old syntax or new docker container syntax? Unfortunately I couldn't find any explanation in the docs.
Also what is the difference between docker container run and docker container create commands? And between docker container stop and docker container kill? The description and syntax are very similar.
Thanks.
As docker grew in features over time and new commands were added cli needed some redesign. You should use docker container exec to be compatible in the future, but docker exec is in fact an alias so until someone decided to deprecate it should also work. If you are interested, you can start reading about this change from this PR: https://github.com/moby/moby/pull/26025

Start up docker container without dockerfile

I've been using Dockerfiles so often that I've forgotten how to start up a new one without one.
I was reading https://docs.docker.com/engine/reference/commandline/start/ and ofc it doesn't state how to start up a new one.
docker run -it ubuntu:16.04 bash
A Dockerfile describes a Docker image not a container.
The container is an instance of this image.
If you want to run a container without building an image (which means without creating a Dockerfile), you need to use an existing image on the Docker Hub (link here).
N.B.: The Docker Hub is a Docker online repository, they are more repositories like Quay, Rancher and others.
For example, if you want to test this, you can use the hello-world image found on the Docker Hub: https://hub.docker.com/_/hello-world/.
According to the documentation, to run a simple hello-world container:
$ docker run hello-world
Source: https://hub.docker.com/_/hello-world/
If you don't have the image locally, Docker will automatically pull it
from the web. If you want to manually pull the image you can run the
following command:
$ docker pull hello-world
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Source: https://hub.docker.com/_/hello-world/
docker start is used to start a stopped container which already exists and in stopped state.
If you want to start a new container use docker run instead. For information about docker run please see https://docs.docker.com/engine/reference/commandline/run/

CI & Docker-in-a-Docker

I am trying to integrate docker into my CI platform. After getting this working properly with a Docker-in-a-docker solution, I came across a blog post by one of the Docker maintainers, where he says that instead of using a Docker-in-a-docker solution for my CI, I should instead simply mount the /var/run/docker.sock to my CI container.
https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
Simply put, when you start your CI container (Jenkins or other), instead of hacking something together with Docker-in-Docker, start it with:
docker run -v /var/run/docker.sock:/var/run/docker.sock ...
So I tried this. I ran the following command:
docker run -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock jenkins
Using jenkins as my CI container.
When running the above command, jenkins starts up properly, and I can jump into the container to see that the docker.sock file is located in the /var/run/ path.
However, when I run the command: docker, the machine returns with the following message:
bash: docker: command not found
Does anyone know what I am missing in order to make this work per the author's instructions?
I am using Docker v. 1.11.1, on a fresh CentOS 7 box.
Thanks in advance
Figured this out today. The above command will work so long as the docker daemon + dependencies are added to the container. In my case, I ended up writing a simple Dockerfile, which also included the line:
RUN curl -sSL https://get.docker.com/ | sh
This installed Docker on the container, and when I ran docker images from within the container, I could see all of the images from my host machine. I am now able to use all of the docker commands from within the container.

How to properly start Docker inside Jenkins that is also running in Docker

I'm trying to run Docker inside a Jenkins container that is also running in Docker (i.e. Docker in Docker). What I want to know is how to properly start the Docker service when booting Jenkins. The only solution I've found today is to build my own Jenkins image based on the official Jenkins image but change the jenkins script loaded by the entry point to also start up Docker:
# I've added this line just before Jenkins is started from the script:
sudo service docker start
# I've also removed "exec" from the original file which used "exec java $JAVA_TOPS ..." but that didn't work
java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS "$#"
This works when I run (using docker run) a new container but the problem is that if I do (docker start) on stopped container the Docker service is not started.
I strongly suspect that this is not the right way to start my Docker service. My plan is to perhaps use supervisord to start Jenkins and Docker separately (I suppose container linking is out of the question since Docker should be executed as a service on the same container that Jenkins is running on?). My concern with this approach is that I'm going to lose the EntryPoint specified in the Jenkins Dockerfile which allows me to pass arguments to the Jenkins container when starting the container, for example:
docker run -p 8080:8080 -v /your/home:/var/jenkins_home jenkins -- <jenkins_arguments>
Does anyone have any recommendations on a good way to solve this preferably by not forking the official Jenkins image?
I'm pretty you cannot do that.
Docker in Docker doesn't mean you have to run docker inside docker with 3 level : host > First level container > Second Level Container
In fact, you just need to share docker with host, and this is your host who will run others containers.
To do that, you have to mount volume with -v parameter
-v /var/run/docker.sock:/var/run/docker.sock
with this command, when you will docker run inside you jenkins container, the docker client will communicate with docker deamon from your host in order to run new container.
To do that, you should run your jenkins container with privileged
--privileged
To resume, here is the full command line
docker run -d -v /var/run/docker.sock:/var/run/docker.sock --privileged myimage
And you you don't need to create a new jenkins image for that.
Hoping to have helped you
http://container-solutions.com/running-docker-in-jenkins-in-docker/

Resources