when i do
docker run consoleapp
I get the output "Hello World"
but when i want to run this program in a docker-compose with -d then nothing happens:
Starting AA... done
Starting BB ... done
Starting consoleapp ... done
Is there a way to see the output?
Try running compose without -d, it will show you the stdout of the containers on the screen, with -d it daemonizes the containers -
$ docker-compose up
In case you want to daemonize it, you can use service name defined in docker compose to get the container logs like -
$ docker-compose up -d ; docker-compose logs consoleapp
Ref - https://docs.docker.com/compose/reference/logs/
Related
I understand that docker start starts a container and docker run creates and starts a container given an image. But naively at first I just ran the command docker start image_name and it just outputs image_name to the console and no container is created and started.
Does docker start image_name do anything besides echo the name to the console? The doc is not very illustrative. If so, what a bad way to fail, better would have been to tell me, that that is not a container but an image and I should first create a container, but maybe I'm missing some useful action which docker start image_name does?
What happens when I run docker start image_name?
$ docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
it just outputs image_name to the console and no container is created and started
a, --attach Attach STDOUT/STDERR and forward signals
The default is not to attach.
Does docker start image_name do anything besides echo the name to the console?
Yes it Start(-s) one or more stopped containers.
I'm missing some useful action which docker docker start image_name does?
It restarts the stopped container.
$ docker run --name work alpine sh -c 'echo important_work ; sleep 10 ; echo success'
important_work
success
$ docker start -a work
important_work
success
Most probably your container that you tested does nothing or expects interactive session to work, and without input it just exits.
Let's say I have started my containers with:
docker-compose -f docker-compose.yml -f docker-compose.override.localhost.yml up --build
Is there a way to lookup, for a given running container, what the command was that started it? Something (pseudocode) like docker origin-command <container id> or similar?
sure you can.
use the inspect docker command:
docker container inspect <container_id>
this command will output a huge json object, what you need is under Config.CMD which appears to be an array containing the container entrypoint command and its flags.
I created a docker container where I install mariadb-server and some more stuff (see screen), but now I want to keep the container running of course, so people can connect to the database server in the container.
The problem is that the container keeps exiting after completed running.
In the last row of the screen you see i tried adding a tail -f, but that also didn't help.
These are the commands I use for building and running:
sudo docker build -t databaseserver .
sudo docker run -it -p 3306:3306 databaseserver
Please help me to just keep it running so i can connect to the running container
You should move much of startup.sh into the Dockerfile.
Then instead of calling service mysql start which starts it in 'daemon mode', you should figure out how to start it interactively.
Then you would not have to tail the logs to keep the container from closing.
At that point you could invoke docker in daemon mode or interactively (your choice) and it should just work right.
You should probably try running the container in daemon mode instead of interactive mode.
So your command should be:
sudo docker run -d -p 3306:3306 databaseserver
CMD servcie mysql start in start-up.sh is async,
mysql -u root < /honepot-project/Database/info.sql may exec failed and exit.
Put the tail -f /dev/null in the dockerfile.
Do it how the MySQL image in the Docker library does it:
https://github.com/docker-library/mysql/blob/3362baccb4352bcf0022014f67c1ec7e6808b8c5/8.0/Dockerfile
The last line of the Dockerfile is:
CMD ["mysqld"]
This will keep the container running (assuming it starts and doesn't stop for any reason), and output any error messages to the logs.
Related to
docker container started in Detached mode stopped after process execution
https://serverfault.com/questions/661909/the-right-way-to-keep-docker-container-started-when-it-used-for-periodic-tasks
I do understand the difference between docker run and create + start, but don't understand how the actual containers created in these two ways differ.
Say I create and run a container with
docker run -dit debian:testing-slim
and then stop it. The created container can later be started with
docker start silly_docker_name
and it'll run in the background, because the entry command for the image is bash.
But when a container is first created
docker create --name silly_name debian:testing-slim
and then started with
docker start silly_name
then it'll exit immediately. Why isn't bash started, or how come it exits in this case?
The difference for a container process that is a shell (like bash in your debian example) is that a shell started without a terminal+interactive "mode" exits without doing anything.
You can test this by changing the command of a create'd container to something that doesn't require a terminal:
$ docker create --name thedate debian date
Now if I run thedate container, each time I run it it outputs the date (in the logs) and exits. docker logs thedate will show this; one entry for each run.
To be explicit, your docker run command has flags -dit: detached, interactive (connect STDIN), and tty are all enabled.
If you want a similar approach with create & start, then you need to allocate a tty for the created container:
$ docker create -it --name ashell debian
Now if I start it, I ask to attach/interactively to it and I get the same behavior as run:
$ docker start -ai ashell
root#6e44e2ae8817:/#
NOTE: [25 Jan 2018] Edited to add the -i flag on create as a commenter noted that as originally written this did not work, as the container metadata did not have stdin connected at the create stage
When I use docker-compose up I can see logs for all containers in my docker-compose.yml file.
However, when I use docker-compose run app I only see console output for app but none of the services that app depends on. How can see log output for the other services?
At the time of writing this the docker-compose run command does not provide a switch to see the logs of other services, hence you need to use the docker-compose logs command to see the logs you want.
Update June 10th 2022
As commented by #Sandburg docker compose is now integrated into docker. As described here most of the docker compose commands can be called the same way just without the dash:
The new Compose V2, which supports the compose command as part of the Docker CLI, is now available.
Compose V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. You can run Compose V2 by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose.
Update July 1st 2019
docker-compose logs <name-of-service>
for all services
docker-compose logs
Use the following options from the documentation:
Usage: logs [options] [SERVICE...]
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output.
-t, --timestamps Show timestamps.
--tail="all" Number of lines to show from the end of the logs
for each container.
See docker logs
You can start Docker compose in detached mode and attach yourself to the logs of all container later. If you're done watching logs you can detach yourself from the logs output without shutting down your services.
Use docker-compose up -d to start all services in detached mode (-d) (you won't see any logs in detached mode)
Use docker-compose logs -f -t to attach yourself to the logs of all running services, whereas -f means you follow the log output and the -t option gives you timestamps (See Docker reference)
Use Ctrl + z or Ctrl + c to detach yourself from the log output without shutting down your running containers
If you're interested in logs of a single container you can use the docker keyword instead:
Use docker logs -t -f <name-of-service>
Save the output
To save the output to a file you add the following to your logs command:
docker-compose logs -f -t >> myDockerCompose.log
If you want to see output logs from all the services in your terminal.
docker-compose logs -t -f --tail <no of lines>
Eg.:
Say you would like to log output of last 5 lines from all service
docker-compose logs -t -f --tail 5
If you wish to log output from specific services then it can be done as below:
docker-compose logs -t -f --tail <no of lines> <name-of-service1> <name-of-service2> ... <name-of-service N>
Usage:
Eg. say you have API and portal services then you can do
something like below :
docker-compose logs -t -f --tail 5 portal api
Where 5 represents last 5 lines from both logs.
Ref: https://docs.docker.com/v17.09/engine/admin/logging/view_container_logs/
use the command to start containers in detached mode:
docker-compose up -d
to view the containers use: docker ps
to view logs for a container: docker logs <containerid>
Unfortunately we need to run docker-compose logs separately from docker-compose run. In order to get this to work reliably we need to suppress the docker-compose run exit status then redirect the log and exit with the right status.
#!/bin/bash
set -euo pipefail
docker-compose run app | tee app.log || failed=yes
docker-compose logs --no-color > docker-compose.log
[[ -z "${failed:-}" ]] || exit 1