Our ducks are trained to go into the coop at dark and lay eggs in nesting boxes. They like and follow a routine.
When the ducks first went into the coop, we had them stay in there for a few days with their food and water. Just before dark we gave them a small healthy duck snack each night. Once they seemed comfortable in their new home we let them out into the run each day. They usually had to be put in the coop just before dark (with a snack) the first week, but then they started going in on their own and waiting for the snack. Once they got into that routine, we started letting them out to free-range and they stayed fairly close to home (coop) going in at dark each night. We stopped the bedtime snack, moving it to earlier in the day and usually outside the coop except for winter. They still go in on their own each night for bed.
When they started laying eggs we had to leave them in the coop longer in the morning so they got used to laying in the coop. Ducks usually lay early in the morning once their cycle gets established. We had nesting boxes at ground level for them. At first they would lay just anywhere, then they started burying them in the bedding in a certain corner of the coop. We persistently moved the eggs each day to the nesting boxes and left them for the next morning. They finally got it, and started laying their eggs in the boxes. Yay! Now every morning we can let them out after the sun is fully in the sky and they have laid us yummy big duck eggs in the boxes.