I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt
Related
As I understood c120809b91b5 == ubuntu
[dev#fedora ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c120809b91b5 ubuntu "bash" 11 days ago Up 39 minutes ubuntuContainer
[dev#fedora ~]$ docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
root#c760c5696300:/# cd testfolder
bash: cd: testfolder: No such file or directory
but why they not similar?
[dev#fedora ~]$ docker exec -it c120809b91b5 bash
(base) root#c120809b91b5:/# cd testfolder
(base) root#c120809b91b5:/testfolder#
What I do wrong?
I need to run c120809b91b5 container with --add-host=host.docker.internal:host-gateway but can't because ubuntu container acts like another with another internal environment.
What is happening here is that with docker run you are creating a new container from the ubuntu image. So when you run docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash you are just creating a new container from the ubuntu image which is given a new containerId: c760c5696300
If you run docker ps -a you will see that you now have two containers with containerIds c120809b91b5 and c760c5696300
Please read docs https://docs.docker.com/engine/reference/run/
try adding "detached"
docker run -d -it --add-host=host.docker.internal:host-gateway ubuntu bash
and then look at the result.
I hope it helps.
You probably have some exited docker containers by now look at them with:
docker ps -a
Step 1:
docker ps -a
container Id: dd5cf6b519b4
I need to inspect inside the stopped docker container which is cannot start.
I tried with docker exec -it container-id bin/bash But this is for running container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
0dfd54557799 ubuntu "/bin/bash" 25 seconds ago Exited (1) 4 seconds ago peaceful_feynman
Commit the stopped image
$ docker commit 0dfd54557799 debug/ubuntu
now we have a new image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debug/ubuntu <none> cc9db32dcc2d 2 seconds ago 64.3MB
create a new container from the "broken" image
$ docker run -it --rm --entrypoint sh debug/ubuntu
inside of the container we can inspect - for example, the file system
$ ls /app
App.dll
App.pdb
App.deps.json
You can start container with specific entrypoint
docker run --entrypoint sleep YOUR_IMAGE 3600
It will block current terminal for 3600 seconds. You can open new terminal tab(do not close current one) and you can verify if your container is working with the
docker ps
If you do not want to block current terminal, you can add -d flag to docker run:
docker run -d --entrypoint sleep YOUR_IMAGE 3600
Above command will start docker which will be doing nothing, then you can ssh into the container when it is working with
docker exec -ti CONTAINER HASH sh
Objective
Essentially what I am trying to accomplish is to install a bunch of software but store the commands run in the Dockerfile in the end. I was planning on recording the installation process by running the "script" function to record the commands run on the command line. I would like to know why it doesn't work but if there is a better way of doing it, I am all ears!
The issue
I'm sure there is a simple answer to this but I can't seem to figure it out. When I create a dummy file within my docker container it dissapears when I exit the container even though the container is running in the background.
Attempt
This is my Dockerfile
##Filename = Dockerfile
FROM centos:7
WORKDIR /dummy_folder
CMD ["echo", "hello world"]
I build the image and run it in the background.
docker build -t my_test_image:v1.0 .
docker run -d e9e949b5d85a tail -f /dev/null
Now I can see my container running in the background
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f4da7a1b74d e9e949b5d85a "tail -f /dev/null" 14 minutes ago Up 14 minutes trusting_poincare
If I create an interactive session and just dump a file in /dummy_folder and exit. When I create a new interactive session /dummy_folder is empty again.
docker run -it e9e949b5d85a /bin/bash
echo "dummy" > dummy
exit
docker run -it e9e949b5d85a /bin/bash
ls -alh /dummy_folder
P.S tail -f /dev/null is just a trick I use to keep the container running in the background as just running it with the flag -d doesn't work for centos containers apparently.
I am running Docker version 19.03.8, build afacb8b
Thanks
Sabri
You're creating a new container every time your run docker run ... . What I think you're trying to do is run a shell on the same container you started with docker run -d e9e949b5d85a tail -f /dev/null. If so, the Docker command your'e looking for is exec
Start an interactive session using the container ID (not the image ID) and do your stuff
docker exec -it 6f4da7a1b74d /bin/bash
$ echo "dummy" > dummy
$ exit
And then check the contents again with exec
docker exec 6f4da7a1b74d ls -alh /dummy_folder
docker ps or docker container ls returns an overview of all running containers. The meaning of all columns is clear to me, except one. What does the column 'COMMAND' mean?
This is the command which is passed to the container.
$ docker run -d busybox top
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eca7c034b21 busybox "top" 6 seconds ago Up 5 seconds recursing_dirac
If you check above, top is the command which has been passed to the busybox container and that's what it's showing in the docker ps -a.
It's the command passed to docker run <image> [command].
$ docker run -d ubuntu sleep 60
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
f0c9cd92a938 ubuntu "sleep 60" 3 seconds ago Up 1 second
If no command was specified there then it's the CMD from the Dockerfile. In ubuntu's case that would be CMD ["/bin/bash"]:
$ docker run -di ubuntu
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
9cd752ee86f4 ubuntu "/bin/bash" 4 seconds ago Up 2 seconds
I am running jenkins docker container. how can keep jenkins backup folder in my current OS ?
You need to use -v flag in docker run this way:
docker run -v /Users/<path>:/<container path>
This will map your /Users/ directory to the container directory specified.
You can find more information here: https://docs.docker.com/engine/tutorials/dockervolumes/
First of all, you need to create a new image from the running container :
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3f279d17e0a jenkins:latest "/bin/bash" 7 days ago Up 25 hours jenkins
$ docker commit jenkins newjenkinsimage:v2
This image takes the exact same state as the running container, check the result with the following command :
$ docker images
REPOSITORY TAG ID CREATED SIZE
newjenkinsimage v2 f5283438590d 16 seconds ago 335.7 MB
Finally you need to run a new container from the new image and mount a volume :
$ docker run -it --name newjenkins -v /path/to/backup/file:/backup newjenkinsimage:v2
PS : for the -v argument, The format is host-src:container-dest