When I run docker-compose up it logs some information to the terminal and I would like to know where this information is coming from and how I might log to it.For example I would like to output each request in a php application within the container. I have tried to look online including the docker docs but have had no luck.
The output in docker-compose is the stdout/stderr from the command the container runs. You see this with docker run if you don't detach, and you can get this from docker logs on a container you're detached from or docker-compose logs from a compose submitted group of containers.
Edit: evidence of this behavior:
$ cat docker-compose.hello-world.yml
version: '2'
services:
hello-world:
image: busybox
command: "echo hello world"
$ docker-compose -f docker-compose.hello-world.yml up
Creating test_hello-world_1
Attaching to test_hello-world_1
hello-world_1 | hello world
test_hello-world_1 exited with code 0
Related
I have 2 docker-compose files.
One of them has a non-default docker-compose file name docker-compose-services.yml.
The second one is docker-compose.yml.
I need to follow logs from time to time for docker-compose-services.yml. Is there an option to follow logs for docker-compose-services.yml or for a project, if I wrap the containers up into one project?
I tried:
docker-compose logs -f - for containers defined in docker-compose.yml.
docker-compose logs --file - does not exist.
docker ps -q | xargs -L 1 docker logs - logs for all running containers. That's not what I need.
To view the logs for non-default docker-compose file (docker-compose-services.yml), you need to specify the file name before the logs command.
Let's say we have a redis container in docker-compose-services.yml
version: "3.3"
services:
redis:
image: "redis:alpine"
We can view the logs using this command
docker-compose -f docker-compose-service.yaml logs -f
I have a docker-compose with just one image. This is the docker-compose.yml definition:
services:
myNodeApp:
image: "1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest"
container_name: 'myNodeApp'
volumes:
- data:/root/data
But I want to move it to docker run as I am using just one container. Executing a docker run command as the following:
docker run 1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest --name myNodeApp -v "data:/root/data"
But I get this message 1.12.4. However, executing docker-compose up starts the application and shows the log by output.
What is the difference? What is the equivalent of docker-compose up with docker? What am I doing differently?
I think you are looking for this?
docker run -it --name myNodeApp -v "data:/root/data"
1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest
Or maybe this command would help you, because it will build a local image associated with the config in your docker-compose.yml .
docker-compose build
docker images
I am trying to speed up a build on Docker for Mac using docker-sync. It takes about 20 minutes to build on Ubuntu, but upwards of 2 hours on a Mac.
I just wanted to go through the workflow to see if it makes sense.
command after the sync has started? Or should all the information in the docker run command above be put into the docker-sync yml files and then I just start the first two docker-sync commands?
Would love some clarity on this. Thanks!
I’ve got a docker run command to start docker that looks like this :
sudo docker run --privileged --mac-address xx:xx:xx:xx:xx:xx -h=“0000000000” -ti --user root --rm -v ~/local/path/to/files/:/path/to/files/on/docker/env
In order to get docker-sync going, I have made a docker-sync.yml file, a docker-compose.yml and a docker-compose-dev.yml, described below.
I had a few questions.
Do I need to run
docker-sync start
and then
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d
and then run my
sudo docker run --privileged --mac-address xx:xx:xx:xx:xx:xx -h=“0000000000” -ti --user root --rm -v ~/local/path/to/files/:/path/to/files/on/docker/env
version: “2”
syncs:
simplest-sync: #tip: add -sync and you keep consistent names as a convention
src: ‘./[unique name of folder containing all source code on local Mac machine]’
my docker-compose.yml looks like this:
version: “2”
services:
app-simplest:
image: alpine
command: [‘watch’, ‘-n3’, ‘cat /var/www/index.html’]
my docker-compose-dev.yml looks like this :
version: “2”
services:
app-simplest:
volumes:
- /path/to/local/Sourcefiles/on/Mac:/path/to/files/on/docker/env
volumes:
simplest-sync:
external: true
I want to be able to eventually enter my docker container and execute the build in the /path/to/files/on/docker/env. It takes a very long time now, and I wish to speed it up. In its current configuration, there is no change to the speed using docker-sync.
Is it possible to pull and start all containers defined in docker-compose.yml? I'm trying to execute docker-compose up -d my/image, where my/image is a repo on DockerHub, but it says "Can't find docker-compose.yml". I also tried first to pull the image using docker pull my/image with the same result
UPD: The image https://hub.docker.com/r/gplcart/core/, source - https://github.com/gplcart/docker-core
SOLVED: It seems docker-compose does not work that way I want. I need to create a directory manually, place docker-compose.yml there, then run docker-compose up.
https://docs.docker.com/compose/wordpress/#define-the-project
I expected that running docker-compose up -d repo/image is enough to download and run all defined containers
To pull an image use docker-compose pull <service_name>, where service_name is one of the services listed in your docker-compose.yml file
The docker pull my/image fails, but should fail with a different error than you noted (you posted a compose error)
In your example, my/name is not a valid service name because you can't use a / in the service name. Compose would give you a different error.
It's unclear to me what my/name represents (assuming you replaced it with something locally).
If you post your docker-compose.yml it would help determine what the correct docker and docker-compose commands should be.
Try logging in to Docker Hub so that Docker Compose knows you want to pull images from there.
From the command line ...
docker login
You will be prompted for a username and password. Once authenticated, compose should pull your images from Docker Hub when running docker-compose up.
Also, you need to run docker-compose up from the same directory where your docker-compose.yml file is. Looking at your docker-compose.yml file on Github, it looks like you are missing a few lines. You need to specify the version, and gplcart, db and phpmyadmin should be under services.
version: '3'
services:
gplcart:
build: .
links:
- db
ports:
- 8080:80
db:
image: mariadb:10.2
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: test
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.7
links:
- db
ports:
- 8181:80
Make sure docker and docker-compose binaries coming from the same package manager. E.g.
$ which -a docker docker-compose
/snap/bin/docker
/snap/bin/docker-compose
In other words, if you've installed Docker from a Snap, docker-compose binary should be already included (ls /snap/docker/current/bin/). When using Apt repository, docker-compose can be installed separately, so don't interchange binaries between Snap with Apt, as well don't mix docker with docker.io package on Apt.
The error Can't find docker-compose.yml indicates that you are not currently in the directory with your docker-compose.yml file, or that you have named the file something different. If you have named the file something different, including a different case or extension, you can either rename the file, or run docker-compose -f your_filename.yml up to pass a different file for docker-compose to parse. If you are not in the directory, make sure to cd into that directory before running docker-compose commands.
docker-compose acts based on your local docker-compose.yml file. Pulling a third-party image with docker-compose is usually useful when, instead of executing separate docker commands (in order to pull an image or deploy your app, etc etc), you want to define your architecture in a more structured way like:
My docker-compose.yml file:
version: '3'
services:
containerA:
image: gplcart/core
containerB:
build: .
# go on defining the rest ...
and deploying with:
docker-compose build && docker-compose up -d
Here is the simplest working example of docker-compose.yml file:
version: '3'
services:
hello-world:
image: hello-world
Which should produce the following results:
$ docker-compose up
Creating network "docker_default" with the default driver
Creating docker_hello-world_1 ... done
Attaching to docker_hello-world_1
hello-world_1 |
hello-world_1 | Hello from Docker!
hello-world_1 | This message shows that your installation appears to be working correctly.
hello-world_1 |
hello-world_1 | To generate this message, Docker took the following steps:
hello-world_1 | 1. The Docker client contacted the Docker daemon.
hello-world_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
hello-world_1 | (amd64)
hello-world_1 | 3. The Docker daemon created a new container from that image which runs the
hello-world_1 | executable that produces the output you are currently reading.
hello-world_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
hello-world_1 | to your terminal.
hello-world_1 |
hello-world_1 | To try something more ambitious, you can run an Ubuntu container with:
hello-world_1 | $ docker run -it ubuntu bash
hello-world_1 |
hello-world_1 | Share images, automate workflows, and more with a free Docker ID:
hello-world_1 | https://hub.docker.com/
hello-world_1 |
hello-world_1 | For more examples and ideas, visit:
hello-world_1 | https://docs.docker.com/get-started/
hello-world_1 |
docker_hello-world_1 exited with code 0
In case of problems, use the following commands which can help to track down the problem:
docker-compose config - Validate configuration file.
docker-compose logs - Check the latest logs.
docker info - Check system-wide information.
sudo strace -fe network docker-compose up - Debug the network issues.
journalctl -u docker.service - Check the logs of Docker service.
I want to start a service with docker-compose and keep the container running so I can get its IP-address via 'docker inspect'. However, the container always exits right after starting up.
I tried to add "command: ["sleep", "60"]" and other things to the docker-compose.yml but whenever I add the line with "command:..." I cant call "docker-compose up" as I will get the message "Cannot start container ..... System error: invalid character 'k' looking for beginning of value"
I also tried adding "CMD sleep 60" and whatnot to the Dockerfile itself but these commands do not seem to be executed.
Is there an easy way to keep the container alive or to fix one of my problems?
EDIT:
Here is the Compose file I want to run:
version: '2'
services:
my-test:
image: ubuntu
command: bash -c "while true; do echo hello; sleep 2; done"
It's working fine If I start this with docker-compose under OS X, but if I try the same under Ubuntu 16.04 it gives me above error message.
If I try the approach with the Dockerfile, the Dockerfile looks like this:
FROM ubuntu:latest
CMD ["sleep", "60"]
Which does not seem to do anything
EDIT 2:
I have to correct myself, turned out it was the same problem with the Dockerfile and the docker-compose.yml:
Each time I add either "CMD ..." to the Dockerfile OR add "command ..." to the compose file, I get above error with the invalid character. If I remove both commands, it works flawlessly.
To keep a container running when you start it with docker-compose, use the following command
command: tail -F anything
In the above command the last part anything should be included literally, and the assumption is that such a file is not present in the container, but with the -F option (capital -F not to be confused with -f which in contrast will terminate immediateley if the file is not found) the tail command will wait forever for the file anything to appear. A forever waiting process is basically what we need.
So your docker-compose.yml becomes
version: '2'
services:
my-test:
image: ubuntu
command: tail -F anything
and you can run a shell to get into the container using the following command
docker exec -i -t composename_my-test_1 bash
where composename is the name that docker-compose prepends to your containers.
You can use tty configuration option.
version: '3'
services:
app:
image: node:8
tty: true # <-- This option
Note: If you use Dockerfile for image and CMD in Dockerfile, this option won't work; however, you can use the entrypoint option in the compose file which clears the CMD from the Dockerfile.
Based on the comment of #aanand on GitHub Aug 26, 2015, one could use tail -f /dev/null in docker-compose to keep the container running.
docker-compose.yml example
version: '3'
services:
some-app:
command: tail -f /dev/null
Why this command?
The only reason for choosing this option was that it received a lot of thumbs up on GitHub, but the highest voted answer does not mean that it is the best answer. The second reason was a pragmatic one as issues had to be solved as soon as possible due to deadlines.
Create a file called docker-compose.yml
Add the following to the file
version: "3"
services:
ubuntu:
image: ubuntu:latest
tty: true
Staying in the same directory, run docker-compose up -d from the terminal
Run docker ps to get the container id or name
You can run docker inspect $container_id
You can enter the container and get a bash shell running docker-compose exec ubuntu /bin/bash or docker-compose exec ubuntu /bin/sh
When done, make sure you are outside the container and run docker-compose down
Here's a small bash script (my-docker-shell.sh) to create the docker compose file, run the container, login to the container and then finally cleanup the docker container and the docker compose file when you log out.
#!/bin/bash
cat << 'EOF' > ./docker-compose.yml
---
version: "3"
services:
ubuntu:
image: ubuntu:latest
command: /bin/bash
# tty: true
...
EOF
printf "Now entering the container...\n"
docker-compose run ubuntu bash
docker-compose down
rm -v ./docker-compose.yml
In the Dockerfile you can use the command:
{CMD sleep infinity}
Some people here write about overwriting the entrypoint so that the command can also have its effect. But no one gives an example. I then:
docker-compose.yml:
version: '3'
services:
etfwebapp:
# For messed up volumes and `sudo docker cp`:
command: "-f /dev/null"
entrypoint: /usr/bin/tail
tty: true
# ...
I am not sure if tty is needed at this point. Is it better to do it twice? In my case it did no harm and worked perfectly. Without entrypoint it didn't work for me because then command had no effect. So I guess for this solution tty is optional.
To understand which command is executed at start-up, simply read the entrypoint before the command (concat with space): /usr/bin/tail -f /dev/null.
I'm late to the party, but you can simply use: stdin_open: true
version: '2'
services:
my-test:
image: ubuntu
stdin_open: true
Blocking command is all you need.
I have been struggling with this problem for half a day.
. There are many answers below, but not clear enough. And nobody said why.
In short, there are two methods, but it can also be said that there is only one, running a Blocking processes in background.
This first one is using COMMAND:
version: '3'
services:
some-app:
command: ["some block command"]
put some block command like sleep infinity, tail -f /dev/null, watch anything, while true ...
Here I recommend sleep infinity.
The second is enable tty=true, then open a shell in command like /bin/bash.
services:
ubuntu:
image: ubuntu:latest
tty: true
command: "/bin/bash"
Since the tty is enabled, bash will keep running background, you can put some other block commands before it if you want.
Be careful, you must excute shell command at the end, like
command: /bin/bash -c "/root/.init-service && /bin/bash"
As you can see, all you need is blocking command.
Just a quick note
I have tested single image based on golang, so when I call docker-compose down here what I get:
version: "3.1"
...
command: tail -f /dev/null # stopping container takes about 10 sec.
tty: true # stopping container takes about 2 sec.
My system info:
Ubuntu 18.04.4 LTS (64-bit)
Docker version 19.03.6, build 369ce74a3c
docker-compose version 1.26.0, build d4451659
As the commenter stated, we'd have to see the Dockerfile in question to give you a complete answer, but this is a very common mistake. I can pretty much guarantee that the command you're trying to run is starting a background process. This might be the command you'd run in non-Docker situations, but it's the wrong thing to do in a Dockerfile. For instance, if what you're running is typically defined as a system service, you might use something like "systemctl start". That would start the process in the background, which will not work. You have to run the process in the foreground, so the entire process will block.
Okay I found my mistake. In the Dockerfile for the image used for compose I specified that the base image should be ubuntu:latest, but I previously created an image called ubuntu by myself and that image did not work. So I did not use the original ubuntu image but rather a corrupt version of my own image also called ubuntu.