How to uninstall a Docker Image - docker

I ran this command docker-compose up -d inside a directory called hosting.
How can I uninstall that image it created, so I can reinstall it.

You can remove the image by using the command:
docker rmi <image-name>:<image-version>
To list all images you can use:
docker images

You can simply again go to the hosting directory and run this:
docker-compose down
Or via -f argument specify the docker-compose file path:
docker-compose -f /path_to/hosting/docker-compose.yml down
When you use this, it stops the created containers and removes them.
Also if you want to update it, just do your work in your project (update the codes, the files and so on), then run this command to build the container:
docker-compose up -d --build

Related

how to reopen a docker container window again

I was able to run a docker container but if I do sudo docker-compose up -d but how to reopen/watch the screen again if I need and close again. I am using ubuntu.
Thanks
In order to follow the logs of all of the containers that are included in the docker-compose.yml file, run the command docker-compose logs -f (probably with sudo in your case) in the same directory in which you already ran sudo docker-compose up -d. You can find more information on the command here.
You are probably looking for docker attach (documentation). Usage is:
docker attach [OPTIONS] CONTAINER

Run DockerCompose File in Background with the specific configuration(yaml) file

I need docker-compose to run all the containers in the background.
I know, if i have one docker-compose.yaml. Then, it is go to the directory in which the configuration file is present and run:
docker-compose up -d
But, In my case, i have 3 docker-compose.yaml files in the configuration folder. And whenever, i run
docker-compose -d docker-compose.yaml up
It doesn't executes/ runs the container.
But, if i run docker-compose -f docker-compose.yaml up. Then, the docker containers will start executing on the foreground terminal.
I want these dockers to run detached from the terminal. How can i achieve that?
Docker has -d option for detached or you can use the usual & at the end of any linux command
docker-compose -f docker-compose.yaml up -d
P.s: It is a good idea to give a name to your container (specially when using -d) using the --name={some_name} so that you can easily get back to that container later using the name
Run below to command to specify docker-compose.yml file including build and send in background;
docker-compose -f docker-compose-1.yml up -d --build
This will also build new image if there are any changes in the project else it will run the same container.

How to list files in a stopped Docker container

This question shows how to copy files out of a stopped container. This requires that I know the full path to the file including its file name. I know the directory I want to copy a file out of, but I do not know its file name since that is generated dynamically. How do I list the files in a directory in a stopped Docker container?
The following Docker command works great if the Docker container is running. But, it fails if the Docker container is stopped.
docker exec --privileged MyContainer ls -1 /var/log
Note: The files are not stored in a persistent volume.
This answer to another question shows how to start a stopped container with another command. Here are the commands to list files in a stopped container.
Commit the stopped container to a new image: test_image.
docker commit $CONTAINER_ID test_image
Run the new image in a new container with a shell.
docker run -ti --entrypoint=sh test_image
Run the list file command in the new container.
docker exec --privileged $NEW_CONTAINER_ID ls -1 /var/log
When starting the container is not an option, you can always export your image (a bit overkill but..) and list its contents:
docker export -o dump.tar <container id>
tar -tvf dump.tar
Reference: Baeldung - Exploring a Docker Container’s Filesystem
The command docker diff *CONTAINER* will list the files added, deleted and changed since the Container started.
If a file did not change since the container was started, then you would have to know the contents of the original image that started the container. So, this answer is not ideal but avoids creating an image and running it.
Unlike container-diff, this command does not require first creating a Docker image.
If you want to see a certain file content, I would suggest using docker container cp command. Here is the doc. It works on stopped container. Example:
docker container cp 02b1ef7de80a:/etc/nginx/conf.d/default.conf ./
This way I got the config file that was generated by templating engine during start.
Try using container-diff with the --type=file option. This will compare two images and report the files added, deleted and modified.
If a file did not change since the container was started, then you would have to know the contents of the original image that started the container. So, this answer is not ideal but avoids creating an image and running it.
This tool requires that you first create an image of the stopped Docker container with docker commit.
Here is the command to install it:
curl -LO https://storage.googleapis.com/container-diff/latest/container-diff-linux-amd64 \
&& chmod +x container-diff-linux-amd64 \
&& mkdir -p $HOME/bin \
&& export PATH=$PATH:$HOME/bin \
&& mv container-diff-linux-amd64 $HOME/bin/container-diff
Here is the command to use the utility:
container-diff analyze $IMAGE --type=file
docker container cp <STOPPED_CONTAINER_ID>:<PATH_TO_FILE> -
Notice the "-" at the end of the command.
It actually "copies" the specified file from the stopped container into "stdout". In other words, it just prints the file contents.
Thanks #azat-khadiev for your direction (I don't know why you got "-1 for that answer...)

How to edit config files in docker container?

I'm running DVWA on a container and I want to change the configuration files of php to allow_url_include option that is disabled by default. I tried to run sudo docker run -t -i my_image /bin/bash and edit files manually. Later on I realized that despite of my editings docker daemon always overwrites them. So I tried to use RUN command in Dockerfile when building the image. Without any sucess. Any thoughts on this?

How to rebuild docker container in docker-compose.yml?

There are scope of services which are defined in docker-compose.yml. These services have been started. I need to rebuild only one of these and start it without up other services.
I run the following commands:
docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it is still running !!!
docker-compose build --no-cache nginx
docker-compose up -d --no-deps # link nginx to other services
At the end I get the old nginx container.
Docker-compose doesn't kill all running containers!
docker-compose up
$ docker-compose up -d --no-deps --build <service_name>
--no-deps - Don't start linked services.
--build - Build images before starting containers.
With docker-compose 1.19 up
docker-compose up --build --force-recreate --no-deps [-d] [<service_name>..]
Without one or more service_name arguments all images will be built if missing and all containers will be recreated.
From the help menu
Options:
-d, --detach Detached mode: Run containers in the background,
print new container names. Incompatible with
--abort-on-container-exit.
--no-deps Don't start linked services.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
--build Build images before starting containers.
Without cache
To force a rebuild to ignore cached layers, we have to first build a new image
docker-compose build --no-cache [<service_name>..]
From the help menu
Options:
--force-rm Always remove intermediate containers.
-m, --memory MEM Set memory limit for the build container.
--no-cache Do not use cache when building the image.
--no-rm Do not remove intermediate containers after a successful build.
Then recreate the container
docker-compose up --force-recreate --no-deps [-d] [<service_name>..]
This should fix your problem:
docker-compose ps # lists all services (id, name)
docker-compose stop <id/name> #this will stop only the selected container
docker-compose rm <id/name> # this will remove the docker container permanently
docker-compose up # builds/rebuilds all not already built container
As #HarlemSquirrel posted, it is the best and I think the correct solution.
But, to answer the OP specific problem, it should be something like the following command, as he doesn't want to recreate ALL services in the docker-compose.yml file, but only the nginx one:
docker-compose up -d --force-recreate --no-deps --build nginx
Options description:
Options:
-d Detached mode: Run containers in the background,
print new container names. Incompatible with
--abort-on-container-exit.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
--build Build images before starting containers.
--no-deps Don't start linked services.
Maybe these steps are not quite correct, but I do like this:
stop docker compose: $ docker-compose down
WARNING: The following prune -a will delete all images, you may not want this as it could effect other projects. you can read more here
remove the container: $ docker system prune -a
start docker compose: $ docker-compose up -d
docker-compose stop nginx # stop if running
docker-compose rm -f nginx # remove without confirmation
docker-compose build nginx # build
docker-compose up -d nginx # create and start in background
Removing container with rm is essential. Without removing, Docker will start old container.
For me it only fetched new dependencies from Docker Hub with both --no-cache and --pull (which are available for docker-compose build.
# other steps before rebuild
docker-compose build --no-cache --pull nginx # rebuild nginx
# other steps after rebuild, e.g. up (see other answers)
The problem is:
$ docker-compose stop nginx
didn't work (you said it is still running). If you are going to rebuild it anyway, you can try killing it:
$ docker-compose kill nginx
If it still doesn't work, try to stop it with docker directly:
$ docker stop nginx
or delete it
$ docker rm -f nginx
If that still doesn't work, check your version of docker, you might want to upgrade.
It might be a bug, you could check if one matches your system/version. Here are a couple, for ex:
https://github.com/docker/docker/issues/10589
https://github.com/docker/docker/issues/12738
As a workaround, you could try to kill the process.
$ ps aux | grep docker
$ kill 225654 # example process id
Simply use :
docker-compose build [yml_service_name]
Replace [yml_service_name] with your service name in docker-compose.yml file. You can use docker-compose restart to make sure changes are effected. You can use --no-cache to ignore the cache.
You can use:
docker-compose build
And if you are using a docker profile:
docker-compose --profile profile_name build
Only:
$ docker-compose restart [yml_service_name]

Resources