Docker will exit right after start
So, before starting the assignment, I understand, as I understand it, for docker.
- Docker has 3 components
- Images
- Containers
- Dockerfile
Now the images are the ones on which the containers are created and the Dockerfile is the stream to be done. In simple words, Images are classes and Containers are image objects.
Now I don't want to take an approach Dockerfile
where you specify the steps to be followed when creating a container.
I want to install some of the core entities on Linux like MongoDb, Redis, etc. and run your server on them.
So I started like this:
-
I downloaded the Ubuntu image from Docker Hub
docker pull ubuntu
which gave me back18261df960118..7a16(big hex key)
-
Now I need to create a container for this image in order to achieve this:
docker create -h abc.com --name abc.com 18261df960118..7a16
which gave me back the container id.
- To enter the container, I have to start it first and then connect to it, so for that, here are the commands
docker start containerId
followeddocker attach containerId
.
But every time he says:
You cannot connect to a stopped container, start it first.
Please help me if I am wrong, this may be new to many, but I am stuck here, please don't overthink the same.
Thanks in advance.
source to share
Edit: In my original post I mention "try to think like with virtual machines". I recently came across this which says not to do this:
Stop thinking of the container as a mini-VM and instead start thinking of it as a simple process.
also noteworthy: Containers are not virtual machines
Original post:
The logic behind Docker containers is that they must have a service and run. If this service stops, they exit and enter the "stopped" state. (When you learn more about Docker, you will understand how it works and you can use ENTRYPOINT
and CMD
). But let's skip that for a while and try to think like virtual machines, start a new container and go inside to type some commands ...
this is done:
docker container create -it --name test ubuntu
445cad0a3afea97494635361316e5869ad3b9ededdd6db46d2c86b4c1461fb75
$ docker container start test
test
$ docker container exec -it test bash
root@445cad0a3afe:/# your are inside, you can type your commands here!
why your failures ...
when you created the container you weren't using a flag -i
that helps Keep STDIN open even if not attached
. This practically means that when starting the container, it uses the set CMD
in the official ubuntu Dockerfile which is bash
and then exits immediately.
docker attach VS docker exec --it bash
You can check this with image type nginx
. If you start a new nginx container and try to connect to it, you will see that the logs from nginx are printed and you cannot enter any command in the shell. This is because the CMD of the image looks like this:
# Define default command.
CMD ["nginx"]
To be able to "attach" to a container like this, but also be able to use a wrapper (some others may also mention this, how to do something equivalent ssh
to a container), you would need to run:
docker container exec -it your_container_name bash
I suggest you also read:
source to share
Check if your Docker container is running by running the command below:
docker ps
If so, you can connect using container-id
as below:
docker attach <docker-container-id>
Container not created?
docker run -d -it <docker-image-name>
In your case, this is: docker run -d -it ubuntu:latest
Attach docker container
docker attach <docker-container-id>
Convert docker container to docker image
Get docker container id like:
docker ps -a
Fix this:
docker commit <docker-container-id> abc-image
Keep it locally or push to remote using docker push
source to share
Dockefile
creates an image, so the instructions Dockerfile
are only executed at build time. The only exception is CMD
and / or ENTRYPOINT
. They instruct the container which command to run when creating the container.
Do not use Dockerfile
to create your own defeat image in the first place, the purpose of using docker. Sure, you can think of containers as a virtual machine and set things up manually, but why not just use virtualbox for that?
I would also highly recommend starting by using the official images from the docker hub . Modify them by expanding these images. It's a lot of work to create great images.
source to share
You are doing the wrong way. To build your own image, you need to use the Dockerfile . Then you can create your image with help Dockerfile
that looks something like this:
FROM <your based image>
RUN <some commands to adapt the based image>
ENTRYPOINT (or CMD) <your command>
To understand the difference between directives ENTRYPOINT
and CMD
go here
It is recommended to use only one process for each container. If you need to run multiple processes, you should take a look at the Docker comp and then make different containers together.
source to share